<?php
/**
 * @author fld 2021-06-17
 * @desc 角色业务类
 */
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class RoleService
{
    private $module_name = "角色管理";
    /**
     * @author fld 2021-06-17
     * @desc 通过id获取一条角色数据
     * @return mixed
     */
    public function getRoleById($id){
        $obj = DB::table('role')->where('id',$id)->first();
        return objectToArray($obj);
    }
    /**
     * @author fld 2021-06-17
     * @desc 通过条件获取一条角色数据
     * @return mixed
     */
    public function getRoleByCondition($condition){
        $obj = DB::table('role')->where($condition)->first();
        return objectToArray($obj);
    }

    /**
     * @author fld 2021-06-18
     * @desc 新增角色信息
     * @param $data
     * @return int
     */
    public function addRole($data){
        return DB::table('role')->insertGetId($data);
    }
    /**
     * @author fld 2021-06-17
     * @desc 根据id更新角色信息
     * @param unknown $id
     * @param unknown $data
     * @return number
     */
    public function updateRoleById($id, $data){
        return DB::table('role')->where('id', $id)->update($data);
    }

    /**
     * @author fld 2021-06-18
     * @desc 获取角色列表信息
     * @return mixed
     */
    public function getRoleList(){
        $list = DB::table('role')->orderBy('id','asc')->get(['id','role_name']);
        return objectToArray($list);
    }

    /**
     * @author fld 2021-06-18
     * @desc 所有角色数组信息
     * @return array
     */
    public function getAllRoleArray(){
        $list = DB::table('role')->orderBy('id','asc')->get(['id','role_name']);
        $role_list = [];
        foreach($list as $v){
            $role_list[$v->id] = $v->role_name;
        }
        return $role_list;
    }
    /**
     * @author fld 2021-06-17
     * @desc 获取角色分页列表
     * @return array
     */
    public function getRoleListByPage(){
        $params = request()->all();
        $condition = $this->getSearchCondition();
        $list = DB::table('role')->where($condition)->paginate(10);
        $pager = $list->links();
        return ['list' => $list, 'pager' => $pager, 'params'=>$params];
    }

    /**
     * @author fld 2021-06-17
     * @desc 查询条件
     * @return array
     */
    public function getSearchCondition(){
        $condition = [];
        $params = request()->all();
        if(!empty($params['keyword'])){
            $condition[] = ['role_name','like','%'.$params['keyword'].'%'];
        }
        return $condition;
    }

    /**
     * @author fld 2021-06-18
     * @desc 新增或更新菜单信息
     * @return array
     */
    public function addOrUpdateRole(){
        $params = request()->all();
        if(empty($params['name'])){
            return ['code' => 1,'message' => '角色名称不能为空'];
        }
        if(empty($params['id'])) {
            $role_info = $this->getRoleByCondition(['role_name' => trim($params['name'])]);
        }else{
            $condition = [];
            $condition[] = ['role_name','=',trim($params['name'])];
            $condition[] = ['id','!=',$params['id']];
            $role_info = $this->getRoleByCondition($condition);
        }
        if (!empty($role_info)) {
            return ['code' => 1, 'message' => '角色名称已存在'];
        }
        DB::beginTransaction();
        $insert_data = [];
        $insert_data['role_name'] = trim($params['name']);
        $insert_data['remark'] = trim($params['remark']);
        $role_id = 0;
        if(!empty($params['id'])){
            $role_id = $params['id'];
            $insert_data['update_time'] = time();
            $res = $this->updateRoleById($params['id'], $insert_data);
            if($res !== false){
                addActionLog(['module_name'=>$this->module_name,'type'=>'edit','log_remark'=>'修改编号:'.$role_id.',角色名称:'.$insert_data['role_name'].'角色信息']);
            }
        }else{
            $insert_data['create_time'] = time();
            $res = $this->addRole($insert_data);
            $role_id = $res;
            if($res !== false){
                addActionLog(['module_name'=>$this->module_name,'type'=>'add','log_remark'=>'新增编号:'.$role_id.',角色名称:'.$insert_data['role_name'].'角色信息']);
            }
        }
        if($res !== false){
            $this->updateRoleMenu($role_id);
            $menuService = new MenuService();
            $menuService->flushRoleMenu($role_id);
            DB::commit();
            return ['code' => 0,'message' => '保存角色成功'];
        }else{
            DB::rollBack();
            return ['code' => 1,'message' => '保存角色失败'];
        }
    }

    /**
     * @author fld 2021-06-18
     * @desc 更新角色菜单关联表
     * @param $role_id
     */
    public function updateRoleMenu($role_id){
        //先清空角色数据
        $res = DB::table('role_menu')->where('role_id',$role_id)->delete();
        if($res !== false){
            $rules = request()->post('rules');
            if(!empty($rules) && count($rules) > 0){
                $insert_data = [];
                $insert_data['role_id'] = $role_id;
                foreach($rules as $k=>$v){
                    $insert_data['menu_id'] = $v;
                    DB::table('role_menu')->insertGetId($insert_data);
                }
            }
        }
    }

    /**
     * @author fld 2021-06-18
     * @desc 根据id删除角色信息
     * @param unknown $id
     * @return number
     */
    public function deleteRoleById($id){
        if($id == 1){
            return ['code' => 1,'message' => '无法删除该角色'];
        }
        $adminService = new AdminService();
        $count = $adminService->getAdminCountByCondition(['role_id'=>$id]);
        if($count > 0){
            return ['code' => 1,'message' => '角色绑定有管理员，无法删除'];
        }
        $roles = $this->getRoleById($id);
        $res = DB::table('role')->where('id', $id)->delete();
        if($res !== false){
            DB::table('role_menu')->where('role_id', $id)->delete();
            addActionLog(['module_name'=>$this->module_name,'type'=>'delete','log_remark'=>'删除编号:'.$id.',角色名称:'.$roles['role_name'].'角色信息']);
            $menuService = new MenuService();
            $menuService->flushRoleMenu($id);
            return ['code' => 0,'message' => '删除角色成功'];
        }else{
            return ['code' => 1,'message' => '删除角色失败'];
        }
    }

    /**
     * @author fld 2021-06-17
     * @desc 通过角色id获取菜单权限
     * @return mixed
     */
    public function getRoleMenuByRoleId($id){
        $list = DB::table('role_menu')->where('role_id',$id)->get(['menu_id']);
        $list = objectToArray($list);
        $role_menu = [];
        foreach($list as $k=>$v){
            $role_menu[] = $v['menu_id'];
        }
        return $role_menu;
    }
}